<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: '标题'
    },
    chartData: {
      type: Object,
      default: {}
    },
    color: {
      type:Array,
      default: function() {
        return ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc']
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
   chartData(newV,oldV) {
     // do something
     this.$nextTick (() => {
       this.initChart();
     })
    } 
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id));
      let chartData = this.chartData;
      let data = chartData.statusList?chartData.statusList.map((item,index) => {
        let d = {
          value : chartData.numbers[index],
          name: item
        }
        return d
      }):[]
      this.chart.setOption({
        backgroundColor:"#fff",
        title: {
            left: 'center',
            text: this.title,
            top: '10px'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        color: this.color,
        legend: {
          right: '1%',
          top: 'center',
          type: 'scroll',
          orient: 'vertical',
        },
        series: [
            {
                name: this.title,
                type: 'pie',
                radius: ['40%', '60%'],
                avoidLabelOverlap: false,
                label: {
                  show: true,
                  formatter: '{b|{b}：}{per|{d}%} ',
                  backgroundColor: '#F6F8FC',
                  borderColor: '#8C8D8E',
                  borderWidth: 1,
                  borderRadius: 4,
                  padding: 4,
                   rich: {
                    b: {
                        color: '#4C5058',
                        fontSize: 14,
                        fontWeight: 'bold',
                        lineHeight: 20
                    },
                    per: {
                        color: '#4C5058',
                        backgroundColor: '#F6F8FC',
                        padding: [3, 4],
                        borderRadius: 4
                    }
                  }
                },
                // emphasis: {
                //     label: {
                //         show: true,
                //         fontSize: '40',
                //         fontWeight: 'bold'
                //     }
                // },
                // labelLine: {
                //     show: false
                // },
                data: data
            }
        ]
      })
    }
  }
}
</script>
